#!/bin/bash
# 监控API Gateway代码变化并自动重启服务

WATCH_DIR="/home/ubuntu/PhotoEnhanceAI-web/1-gateway/02-api-gateway"
LOG_FILE="/home/ubuntu/PhotoEnhanceAI-web/1-gateway/shared/logs/auto-reload.log"

echo "[$(date)] 🔍 开始监控 $WATCH_DIR" >> "$LOG_FILE"

inotifywait -m -e modify,create,move     --include '.*\.py$'     "$WATCH_DIR" | while read -r directory events filename; do
    
    echo "[$(date)] 📝 检测到文件变化: $directory$filename" >> "$LOG_FILE"
    echo "[$(date)] 🔄 等待5秒后重启服务..." >> "$LOG_FILE"
    
    # 5秒冷却期，避免频繁重启
    sleep 5
    
    # 重启服务
    sudo systemctl restart photoenhance-gateway
    
    if [ $? -eq 0 ]; then
        echo "[$(date)] ✅ 服务重启成功" >> "$LOG_FILE"
    else
        echo "[$(date)] ❌ 服务重启失败" >> "$LOG_FILE"
    fi
    
    echo "---" >> "$LOG_FILE"
done
